package daos

import (
	"gorm.io/gorm"
	"sanrenx.com/xyxf/app/models"
	"time"
)

type OpposDao struct {
	DB *gorm.DB
}

/**
 * @Description: 获取运营详情
 * @receiver dao
 * @param id 运营位ID
 * @return data 运营数据
 * @return err 异常信息
 */
func (dao *OpposDao) GetOppos(id uint64) (data *models.OpposDetails, err error) {

	if err = dao.DB.Unscoped().
		Table("oppos").
		Where("id = ?", id).
		First(&data).
		Error; err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return data, nil
}

/**
 * @Description: 获取运营位
 * @receiver dao
 * @param module 模块
 * @param mode 模式
 * @return ids 运营位ID
 * @return list 运营位条目
 * @return err 异常信息
 */
func (dao *OpposDao) GetOpposByModuleMode(module string, mode string) (list []*models.Oppos, err error) {

	if err = dao.DB.Table("oppos").
		Select([]string{
			"title",
			"image_url",
			"jump_url",
		}).Where("module = ?", module).  //模块
		Where("mode = ?", mode).         //模式
		Where("due_at > ?", time.Now()). //过期时间
		Where("deleted_at is NULL").     //未删除
		Order("weight DESC").            //热度倒序
		Limit(12).                       //最多12条
		Find(&list).
		Error; err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return list, nil
}
